package com.qst.itoffer.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;


@SuppressWarnings("ALL")
public class CookieEncryptTool {

    /**
     * Base64加密
     *
     * @param cleartext
     * @return
     */
    public static String encodeBase64(String cleartext) {

        try {
            cleartext = new String(Base64.encodeBase64(cleartext
                    .getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return cleartext;
    }

    /**
     * Base64解密
     *
     * @param ciphertext
     * @return
     */
    public static String decodeBase64(String ciphertext) {
        try {
            ciphertext = new String(Base64.decodeBase64(ciphertext.getBytes()),
                    "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ciphertext;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "测试abc123";
        String encode = CookieEncryptTool.encodeBase64(str);
        System.out.println(encode);

        String decode = CookieEncryptTool.decodeBase64(encode);
        System.out.println(decode);
    }
}
